package mpcs.utils.net
{
	import air.net.SocketMonitor;
	import air.net.URLMonitor;
	
	import flash.events.StatusEvent;
	import flash.net.URLRequest;
	
	import mpcs.ApplicationFacade;
	import mpcs.NotiConst;
	import mpcs.utils.xml.XMLUtils;

	/**
	 *  网络连接检测工具类
	 * @author zhangzuoqiang
	 */	
	public class NetMonitor {
		
		private static var socketMonitor:SocketMonitor;
		private static var monitor:URLMonitor;
		private static var monitorXML:XML;
		
		/** 记录是否已初始化 **/
		public static var initFlag:Boolean = false;
		
		/**记录检测次数**/
		public static var MonitorCount:int = 0;
		
		/**当前网络连接状况**/
		public static var CurrentState:Boolean = false;
		
		public static function init3333():void{
			
			initFlag = true;
			
			monitorXML = XMLUtils.loadXMLFile("data/netmonitor.xml");
			var urlString:String = monitorXML.urlmonitor.@url;
			
			// URLRequest that the Monitor Will Check
			var url:URLRequest = new URLRequest( urlString );
			// Checks Only the Headers - Not the Full Page
			url.method = "HEAD";
			
			// Create the URL Monitor and Pass it the URLRequest
			monitor = new URLMonitor(url);
			// Set the Interval (in ms) - 3000 = 3 Seconds
			monitor.pollInterval = 3000;
			// Add Our Event Listener to Respond the a Change in Connection Status
			monitor.addEventListener(StatusEvent.STATUS,on_connection);
			// Start the URLMonitor
			monitor.start();
		}
		
		private static function on_connection(event:StatusEvent):void {
			if(monitor.available) {
				trace("网络已连接 。。。");
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_SUCCESS );
				CurrentState = true;
			} else {
				trace("网络连接故障 。。。");
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_FAILED );
				CurrentState = true;
			}
		}
		
		
		/**此方法存在bug**/
		public static function init2():void{
			
			initFlag = true;
			
			monitorXML = XMLUtils.loadXMLFile("data/netmonitor.xml");
			var url1:String = monitorXML.socketmonitor.@url;
			var port:int = int( monitorXML.socketmonitor.@port );
			var url2:String = monitorXML.urlmonitor.@url;
			
			trace("SocketMonitor: " + url1 + " port: " + port);
			trace("URLMonitor: " + url2);
			
			socketMonitor = new SocketMonitor( url1 ,port );
			socketMonitor.addEventListener(StatusEvent.STATUS, socketStatusChange);
			socketMonitor.start();
			
			monitor = new URLMonitor(new URLRequest( url2 ));
			monitor.addEventListener(StatusEvent.STATUS, announceStatus);
			monitor.start();
		}
		
		private static function announceStatus(e:StatusEvent):void {
			if( monitor.available ){
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_SUCCESS );
				trace("网络已连接 。。。");
				MonitorCount++;
				CurrentState = true;
			}else{
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_FAILED );
				if(MonitorCount == 0)
					return;
				trace("网络连接故障 。。。");
				CurrentState = false;
			}
		}
		
		private static function socketStatusChange(e:StatusEvent):void {
			if( socketMonitor.available ){
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_SUCCESS );
				trace("网络连接成功 。。。");
				MonitorCount++;
				CurrentState = true;
			}else{
				ApplicationFacade.getInstance().sendNotification( NotiConst.NET_CONNECT_FAILED );
				if(MonitorCount == 0)
					return;
				trace("网络连接断开 。。。");
				CurrentState = false;
			}
		}
		
		/**
		 * 清除监听器
		 */
		public static function disposeMonitorListener():void {
			socketMonitor.removeEventListener(StatusEvent.STATUS, announceStatus);
			monitor.removeEventListener(StatusEvent.STATUS, announceStatus);
		}
	}
}